package com.google.code.synchronized_player;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Track {
	private String path;
	private String hash;
	
	public Track(String path) {
		this.path = path;
		this.hash = getHash(path);
	}
	
	public String getPath() {
		return path;
	}
	
	public String getHash() {
		return hash;
	}
	
	public String getName() {
		return getName(path);
	}
	
	public static String getName(String path) {
		String pathItems[] = path.split(File.separator);
		return pathItems[pathItems.length - 1];
	}
	

	
	public static String getHash(String path) {
		String s = getName(path) + Long.toString(new File(path).length());
		try {
			MessageDigest md = MessageDigest.getInstance("md5");
			md.reset();
//			byte sbytes[] = s.getBytes();
//			String res = "";
//			for (int i = 0; i < sbytes.length; i++) {
//				res += Byte.valueOf(sbytes[i]) + " ";
//	        }
//			Log.d("Track", res);
			
			md.update(s.getBytes());
			byte hash[] = md.digest();
			String base64String = Base64.encodeToString(hash, Base64.DEFAULT);

//			String res = "";
//			for (int i = 0; i < hash.length; i++) {
//				int dig1 = 0x0F & (hash[i] >> 4);
//				int dig2 = (0x0F & hash[i]);
//				res += (char)(dig1 >= 10 ? 'a' + dig1 - 10 : '0' + dig1);
//				res += (char)(dig2 >= 10 ? 'a' + dig2 - 10 : '0' + dig2);
//	        }
			return base64String.substring(0, base64String.length() - 1);
		} catch(NoSuchAlgorithmException nsaex) {
			
		}
		return null;
	}
}
